﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace IceCS.Clicker.Core
{
    /// <summary>
    /// 鼠标点击器
    /// </summary>
    public class MouseCliker
    {
        /// <summary>
        /// 如果没有使用MOUSEEVENTF_ABSOLUTE，函数默认的是相对于鼠标当前位置的点，
        /// 如果dx，和dy，用0，0表示，这函数认为是当前鼠标所在的点。
        /// 
        /// </summary>
        /// <param name="dwFlags">MouseKey 或者组合</param>
        /// <param name="dx">根据MOUSEEVENTF_ABSOLUTE标志，指定 x 方向的绝对位置或相对位置</param>
        /// <param name="dy">根据MOUSEEVENTF_ABSOLUTE标志，指定 y 方向的绝对位置或相对位置</param>
        /// <param name="cButtons">没有使用</param>
        /// <param name="dwExtraInfo">没有使用</param>
        /// <returns></returns>
        [DllImport("user32")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        /// <summary>
        /// 左键点击
        /// </summary>
        /// <param name="count">点击次数</param>
        public static void LeftClick(int count = 1)
        {
            int flag = MouseKey.MOUSEEVENTF_LEFTDOWN | MouseKey.MOUSEEVENTF_LEFTUP;
            for (int i = 0; i < count; i++)
            {
                Task.Delay(100).Wait();
                mouse_event(flag, 0, 0, 0, 0);
            }
        }

        /// <summary>
        /// 右键点击
        /// </summary>
        /// <param name="count">点击次数</param>
        public static void RightClick(int count = 1)
        {
            int flag = MouseKey.MOUSEEVENTF_RIGHTDOWN | MouseKey.MOUSEEVENTF_RIGHTUP;
            for (int i = 0; i < count; i++)
            {
                mouse_event(flag, 0, 0, 0, 0);
            }
        }

        /// <summary>
        /// 移动鼠标
        /// </summary>
        /// <param name="fromX"></param>
        /// <param name="fromY"></param>
        /// <param name="toX"></param>
        /// <param name="toY"></param>
        public static void Move(int fromX, int fromY, int toX, int toY)
        {
            int flag = MouseKey.MOUSEEVENTF_ABSOLUTE | MouseKey.MOUSEEVENTF_MOVE;
            mouse_event(flag, fromX, fromY, 0, 0);
            mouse_event(flag, toX, toY, 0, 0);
        }


    }
}
